package com.mrchip.notification_toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.TaskStackBuilder;
import androidx.core.content.ContextCompat;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button mToastShort;
    private Button mToastLong;
    private Button mNotificationShort;
    private Button mNotificationLong;
    private Button mNotificationClear;
    private int id = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToastShort = findViewById(R.id.btToastShort);
        mToastLong = findViewById(R.id.btToastLong);
        mNotificationShort = findViewById(R.id.btNotNormal);
        mNotificationLong = findViewById(R.id.btNotMoreText);
        mNotificationClear = findViewById(R.id.btNotClear);

        mToastShort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getBaseContext(), "Short Toast Message", Toast.LENGTH_SHORT).show();
            }
        });

        mToastLong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getBaseContext(), "Long Toast Message", Toast.LENGTH_LONG).show();
            }
        });

        mNotificationShort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                notification(view);
            }
        });

        mNotificationClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cleanNotification(view);
            }
        });

        mNotificationLong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                notification_long(view);
            }
        });
    }



    public void notification(View view) {
        String CHANNEL_ID = "my_channel_01";
        CharSequence name = "my_channel";
        String Description = "This is my channel";
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.ic_launcher_foreground);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher);
        try {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

                int importance = NotificationManager.IMPORTANCE_HIGH;
                NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
                mChannel.setDescription(Description);
                mChannel.enableLights(true);
                mChannel.setLightColor(Color.RED);
                mChannel.enableVibration(true);
                mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                mChannel.setShowBadge(false);
                notificationManager.createNotificationChannel(mChannel);
            }

            Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);

            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);


            Intent resultIntent = new Intent(this, MainActivity.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

            mBuilder.setContentIntent(resultPendingIntent);
//            notificationManager.notify(id, builder.build());
            //设置小图标
            mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
            //设置大图标
            mBuilder.setLargeIcon(bitmap);
            //设置标题
            mBuilder.setContentTitle("Title");
            //设置通知正文
            mBuilder.setContentText("This is Content: Notification Num " + id++);
            //设置摘要
            mBuilder.setSubText("Abstract");
            //设置是否点击消息后自动clean
            mBuilder.setAutoCancel(true);
            //显示指定文本
            mBuilder.setContentInfo("Info");
            //与setContentInfo类似，但如果设置了setContentInfo则无效果
            //用于当显示了多个相同ID的Notification时，显示消息总数
            mBuilder.setNumber(2);
            //通知在状态栏显示时的文本
            mBuilder.setTicker("Text on status bar");
            //设置优先级
            mBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
            //自定义消息时间，以毫秒为单位，当前设置为比系统时间少一小时
            mBuilder.setWhen(System.currentTimeMillis() - 3600000);
            //设置为一个正在进行的通知，此时用户无法清除通知
            mBuilder.setOngoing(true);
            //设置消息的提醒方式，震动提醒：DEFAULT_VIBRATE     声音提醒：NotificationCompat.DEFAULT_SOUND
            //三色灯提醒NotificationCompat.DEFAULT_LIGHTS     以上三种方式一起：DEFAULT_ALL
            mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
            //设置震动方式，延迟零秒，震动一秒，延迟一秒、震动一秒
            mBuilder.setVibrate(new long[]{0, 1000, 1000, 1000});

            notificationManager.notify(1, mBuilder.build());
        } catch (OutOfMemoryError e) {
            // Handle the error

        } catch (NullPointerException e) {
            //Handle null pointer error
        }

    }


    public void notification_long(View view) {
        String CHANNEL_ID = "my_channel_01";
        CharSequence name = "my_channel";
        String Description = "This is my channel";
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.ic_launcher_foreground);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher);
        try {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

                int importance = NotificationManager.IMPORTANCE_HIGH;
                NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
                mChannel.setDescription(Description);
                mChannel.enableLights(true);
                mChannel.setLightColor(Color.RED);
                mChannel.enableVibration(true);
                mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                mChannel.setShowBadge(false);
                notificationManager.createNotificationChannel(mChannel);
            }

            Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);

            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);


            Intent resultIntent = new Intent(this, MainActivity.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

            mBuilder.setContentIntent(resultPendingIntent);
//            notificationManager.notify(id, builder.build());
            //设置小图标
            mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
            //设置大图标
            mBuilder.setLargeIcon(bitmap);
            //设置标题
            mBuilder.setContentTitle("Title");
            //设置通知正文
            mBuilder.setContentText("This is Content: Notification Num " + id++);
            //设置摘要
            mBuilder.setSubText("Abstract");
            //设置是否点击消息后自动clean
            mBuilder.setAutoCancel(true);
            //显示指定文本
            NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
            style.bigText("This can be a very long sentence.\nThis can be a very long sentence.\nThis can be a very long sentence.\nThis can be a very long sentence.\nThis can be a very long sentence.\nThis can be a very long sentence.\nThis can be a very long sentence.\nThis can be a very long sentence.\nThis can be a very long sentence.\nThis can be a very long sentence.\n");
            style.setBigContentTitle("Title onClicked");
            style.setSummaryText("This is Summary");
            mBuilder.setStyle(style);
            mBuilder.setContentInfo("Info");
            //与setContentInfo类似，但如果设置了setContentInfo则无效果
            //用于当显示了多个相同ID的Notification时，显示消息总数
            mBuilder.setNumber(2);
            //通知在状态栏显示时的文本
            mBuilder.setTicker("Text on status bar");
            //设置优先级
            mBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
            //自定义消息时间，以毫秒为单位，当前设置为比系统时间少一小时
            mBuilder.setWhen(System.currentTimeMillis() - 3600000);
            //设置为一个正在进行的通知，此时用户无法清除通知
            mBuilder.setOngoing(true);
            //设置消息的提醒方式，震动提醒：DEFAULT_VIBRATE     声音提醒：NotificationCompat.DEFAULT_SOUND
            //三色灯提醒NotificationCompat.DEFAULT_LIGHTS     以上三种方式一起：DEFAULT_ALL
            mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
            //设置震动方式，延迟零秒，震动一秒，延迟一秒、震动一秒
            mBuilder.setVibrate(new long[]{0, 1000, 1000, 1000});

            notificationManager.notify(2, mBuilder.build());
        } catch (OutOfMemoryError e) {
            // Handle the error

        } catch (NullPointerException e) {
            //Handle null pointer error
        }

    }





    public void cleanNotification(View view) {
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancelAll();
        //mNotificationManager.cancel(1);
    }
}
